<template>
  <div class="chat-area">
    <!-- 聊天界面 -->
    <div class="chat-container">
      <!-- 聊天头部 -->
      <div class="chat-header">
        <div class="header-content">
          <h3>💬 AI理疗助手</h3>
          <p v-if="currentSession">{{ currentSession.title }}</p>
          <p v-else>选择或创建会话开始对话</p>
        </div>
      </div>
      
      <!-- 聊天历史 -->
      <div class="chat-history" ref="chatHistory">
        <div v-if="conversations.length === 0" class="empty-chat">
          <div class="empty-icon">🤖</div>
          <p>还没有对话记录</p>
          <p>在下方输入框开始您的第一次咨询</p>
        </div>
        
        <div v-else>
          <div 
            v-for="chat in conversations" 
            :key="chat.id"
            class="message-group"
          >
            <!-- 用户消息 -->
            <div class="message user-message">
              <div class="message-content">
                {{ chat.user_input }}
              </div>
              <div class="message-time">
                {{ formatTime(chat.created_at) }}
              </div>
            </div>
            
            <!-- AI回复 -->
            <div v-if="chat.agent_response" class="message ai-message">
              <div class="ai-avatar">🤖</div>
              <div class="message-body">
                <div class="message-content">
                  <div v-html="formatMessage(chat.agent_response)"></div>
                </div>
                <div class="message-time">
                  {{ formatTime(chat.created_at) }}
                </div>
              </div>
            </div>
            
            <!-- 加载中的AI回复 -->
            <div v-else-if="chat.isWaitingReply" class="message ai-message">
              <div class="ai-avatar">🤖</div>
              <div class="message-body">
                <div class="message-content loading">
                  <div class="typing-indicator">
                    <span></span>
                    <span></span>
                    <span></span>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
      
      <!-- 输入区域 -->
      <div class="input-area">
        <div class="input-container">
          <textarea
            v-model="inputMessage"
            @keydown.enter.exact.prevent="sendMessage"
            @keydown.enter.shift.exact="newLine"
            placeholder="请描述您的理疗需求或身体不适症状..."
            class="message-input"
            :disabled="loading || !currentSession"
            rows="2"
            ref="messageInput"
          ></textarea>
          <button 
            @click="sendMessage" 
            :disabled="!inputMessage.trim() || loading || !currentSession"
            class="send-btn"
          >
            {{ loading ? '发送中...' : '发送' }}
          </button>
        </div>
        <div class="input-hint">
          <span v-if="!currentSession">请先选择或创建一个会话</span>
          <span v-else>Enter发送 | Shift+Enter换行</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { formatTime } from '../utils/date.js'

export default {
  name: 'ChatArea',
  props: {
    currentSession: {
      type: Object,
      default: null
    },
    conversations: {
      type: Array,
      default: () => []
    },
    loading: {
      type: Boolean,
      default: false
    }
  },
  emits: ['send-message'],
  data() {
    return {
      inputMessage: ''
    }
  },
  watch: {
    conversations: {
      handler() {
        this.$nextTick(() => {
          this.scrollToBottom()
        })
      },
      deep: true
    }
  },
  methods: {
    formatTime,
    
    sendMessage() {
      if (!this.inputMessage.trim() || this.loading) return
      
      this.$emit('send-message', this.inputMessage.trim())
      this.inputMessage = ''
      this.adjustTextareaHeight()
    },
    
    newLine() {
      // Shift + Enter 时自动换行，由浏览器默认处理
    },
    
    formatMessage(text) {
      // 简单的文本格式化，将换行符转换为<br>
      return text.replace(/\n/g, '<br>')
    },
    
    scrollToBottom() {
      if (this.$refs.chatHistory) {
        this.$refs.chatHistory.scrollTop = this.$refs.chatHistory.scrollHeight
      }
    },
    
    adjustTextareaHeight() {
      this.$nextTick(() => {
        const textarea = this.$refs.messageInput
        if (textarea) {
          textarea.style.height = 'auto'
          textarea.style.height = Math.min(textarea.scrollHeight, 120) + 'px'
        }
      })
    }
  },
  mounted() {
    // 监听输入框变化，自动调整高度
    if (this.$refs.messageInput) {
      this.$refs.messageInput.addEventListener('input', this.adjustTextareaHeight)
    }
  }
}
</script>

<style scoped>
.chat-area {
  height: 100%;
  display: flex;
  flex-direction: column;
}

/* 聊天头部 */
.chat-header {
  background: linear-gradient(135deg, #00b894, #00a085);
  color: white;
  padding: 20px;
  border-bottom: 1px solid #e9ecef;
}

.header-content h3 {
  font-size: 18px;
  margin-bottom: 5px;
  font-weight: 600;
}

.header-content p {
  font-size: 14px;
  opacity: 0.9;
  margin: 0;
}

/* 聊天界面 */
.chat-container {
  height: 100%;
  display: flex;
  flex-direction: column;
}

.chat-history {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
  background: #f8f9fa;
  min-height: 0;
}

.empty-chat {
  text-align: center;
  color: #636e72;
  padding: 60px 20px;
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.empty-icon {
  font-size: 48px;
  margin-bottom: 20px;
}

.empty-chat p {
  margin: 5px 0;
  font-size: 14px;
}

.message-group {
  margin-bottom: 20px;
}

.message {
  margin-bottom: 10px;
}

.user-message {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
}

.user-message .message-content {
  background: #00b894;
  color: white;
  padding: 12px 16px;
  border-radius: 18px 18px 4px 18px;
  max-width: 70%;
  word-wrap: break-word;
}

.ai-message {
  display: flex;
  align-items: flex-start;
  gap: 10px;
}

.ai-avatar {
  width: 32px;
  height: 32px;
  background: #e9ecef;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  flex-shrink: 0;
}

.message-body {
  flex: 1;
  max-width: 70%;
}

.ai-message .message-content {
  background: white;
  padding: 12px 16px;
  border-radius: 18px 18px 18px 4px;
  border: 1px solid #e9ecef;
  word-wrap: break-word;
}

.message-time {
  font-size: 12px;
  color: #636e72;
  margin-top: 4px;
}

.user-message .message-time {
  text-align: right;
}

/* 加载动画 */
.loading {
  padding: 16px !important;
}

.typing-indicator {
  display: flex;
  gap: 4px;
}

.typing-indicator span {
  width: 8px;
  height: 8px;
  background: #636e72;
  border-radius: 50%;
  animation: typing 1.4s infinite ease-in-out;
}

.typing-indicator span:nth-child(1) {
  animation-delay: 0s;
}

.typing-indicator span:nth-child(2) {
  animation-delay: 0.2s;
}

.typing-indicator span:nth-child(3) {
  animation-delay: 0.4s;
}

@keyframes typing {
  0%, 80%, 100% {
    opacity: 0.3;
    transform: scale(0.8);
  }
  40% {
    opacity: 1;
    transform: scale(1);
  }
}

/* 输入区域 */
.input-area {
  background: white;
  border-top: 1px solid #e9ecef;
  padding: 25px;
  box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.05);
}

.input-container {
  display: flex;
  gap: 12px;
  align-items: flex-end;
}

.message-input {
  flex: 1;
  border: 2px solid #e9ecef;
  border-radius: 12px;
  padding: 15px 18px;
  font-size: 14px;
  resize: none;
  outline: none;
  transition: border-color 0.2s;
  min-height: 50px;
  max-height: 120px;
  line-height: 1.4;
}

.message-input:focus {
  border-color: #00b894;
}

.message-input:disabled {
  background: #f8f9fa;
  color: #636e72;
}

.send-btn {
  background: #00b894;
  color: white;
  border: none;
  border-radius: 12px;
  padding: 15px 25px;
  font-size: 14px;
  cursor: pointer;
  transition: background 0.2s;
  height: 50px;
  font-weight: 500;
  min-width: 80px;
}

.send-btn:hover:not(:disabled) {
  background: #00a085;
}

.send-btn:disabled {
  background: #e9ecef;
  color: #636e72;
  cursor: not-allowed;
}

.input-hint {
  text-align: center;
  font-size: 12px;
  color: #636e72;
  margin-top: 10px;
}

/* 响应式适配 */
@media (max-width: 1024px) {
  .chat-area {
    height: 100%;
  }
  
  .chat-header {
    padding: 15px;
  }
  
  .input-area {
    padding: 20px;
  }
  
  .message-input {
    padding: 12px 15px;
    min-height: 40px;
  }
  
  .send-btn {
    padding: 12px 20px;
    height: 40px;
    min-width: 70px;
  }
}
</style>